#include <iostream>

int atoi(char *str)
{
    char *p = str;
    int sign = 1;

//去除前置空白
    while (*p == ' ') {
        p++;
    }

//判断符号
    if (*p == '-') {
        sign = -1;
        p++;
    } else if (*p == '+') {
        p++;
    }

//去除前置0
    while (*p == '0') {
        p++;
    }


    int sum = 0;

    while (*p != '\0') {
        if ('0' <= *p && *p <= '9') {
            sum = sum * 10 + *p - '0';
        } else {//非数字则退出
            break;
        }

        ++p;
    }

    return sign * sum;
}

int main(int argc, const char *argv[])
{
    char strs[][256] = {
        "12345",
        "00012345",
        "-213",
        "-0000213",
        "+233",
        "+000233",
        "   1234",
        "   00001234",
        "   +1234",
        "   +0001234",
        "     -234",
        "     -0000234",
        "dsaf0000234",
        "    dsaf0000234",
        "    234 sfasd",
        "    234sfasddfa12223",
        "    234sfasddfa122233243fdsafsd"
    };

    for (int i = 0; i < sizeof(strs) / sizeof(strs[0]) - 1; ++i) {
        std::cout << strs[i] << " = " << atoi(strs[i]) << std::endl;
    }

    return 0;
}

